﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using DotNetNuke.Common.Utilities;

namespace SBPweb.DNN.PhotoGallery
{
    /// <summary>
    /// Summary description for SqlDataProvider
    /// </summary>
    public class SqlDataProvider : DataProvider
    {

        private static string _ProviderType = "data";
        private DotNetNuke.Framework.Providers.ProviderConfiguration _providerConfiguration = DotNetNuke.Framework.Providers.ProviderConfiguration.GetProviderConfiguration(_ProviderType);
        private string _connectionString;
        private string _providerPath;
        private string _objectQualifier;
        private string _databaseOwner;

        public SqlDataProvider()
        {
            DotNetNuke.Framework.Providers.Provider objProvider = ((DotNetNuke.Framework.Providers.Provider)(_providerConfiguration.Providers[_providerConfiguration.DefaultProvider]));
            // This code handles getting the connection string from either the connectionString / appsetting section and uses the connectionstring section by default if it exists.
            // Get Connection string from web.config
            _connectionString = Config.GetConnectionString();// If above funtion does not return anything then connectionString must be set in the dataprovider section.
            if ((_connectionString == ""))
            {
                // Use connection string specified in provider
                _connectionString = objProvider.Attributes["connectionString"];
            }
            _providerPath = objProvider.Attributes["providerPath"];
            _objectQualifier = objProvider.Attributes["objectQualifier"];
            if (_objectQualifier != "" & _objectQualifier.EndsWith("_") == false)
            {
                _objectQualifier += "_";
            }
            _databaseOwner = objProvider.Attributes["databaseOwner"];
            if (_databaseOwner != "" & _databaseOwner.EndsWith(".") == false)
            {
                _databaseOwner += ".";
            }
        }

        public string ConnectionString
        {
            get
            {
                return _connectionString;
            }
        }
        public string ProviderPath
        {
            get
            {
                return _providerPath;
            }
        }
        public string ObjectQualifier
        {
            get
            {
                return _objectQualifier;
            }
        }
        public string DatabaseOwner
        {
            get
            {
                return _databaseOwner;
            }
        }
        private object GetNull(object Field)
        {
            return DotNetNuke.Common.Utilities.Null.GetNull(Field, DBNull.Value);
        }

        //public override IDataReader GetPhotoGalleries(int ModuleId)
        //{
        //    return ((IDataReader)(SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "GetPhotoGalleries", ModuleId)));
        //}
        public override IDataReader GetPhotoGallery(int? PhotoGalleryId, int? ModuleId)
        {
            return ((IDataReader)(SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "GetPhotoGallery", PhotoGalleryId, ModuleId)));
        }
        public override int AddPhotoGallery(int ModuleId, string Title, string Source, string BasePath, string Width, string Height, string Background, string Foreground, string ButtonBackground, string ButtonForeground, string ButtonBorder, string ButtonPlayForeground, string CaptionBackground, string CaptionForeground, string ContentBackground, string SelectedThumbnailBorder, string TrackerForeground, decimal? CaptionOpacity, string CaptionFont, string TrackerFont, int? BorderRadius, int? Padding, bool? CaptionVisible, bool? ThumbnailsVisible, bool? TrackerVisible, bool? AutoPlay)
        {
            return (Convert.ToInt32((SqlHelper.ExecuteScalar(ConnectionString, DatabaseOwner + ObjectQualifier + "AddPhotoGallery",
                ModuleId,
                Title,
                Source,
                BasePath,
                Width,
                Height,
                Background,
                Foreground,
                ButtonBackground,
                ButtonForeground,
                ButtonBorder,
                ButtonPlayForeground,
                CaptionBackground,
                CaptionForeground,
                ContentBackground,
                SelectedThumbnailBorder,
                TrackerForeground,
                CaptionOpacity,
                CaptionFont,
                TrackerFont,
                BorderRadius,
                Padding,
                CaptionVisible,
                ThumbnailsVisible,
                TrackerVisible,
                AutoPlay))));
        }
        public override void UpdatePhotoGallery(int PhotoGalleryId, int ModuleId, string Title, string Source, string BasePath, string Width, string Height, string Background, string Foreground, string ButtonBackground, string ButtonForeground, string ButtonBorder, string ButtonPlayForeground, string CaptionBackground, string CaptionForeground, string ContentBackground, string SelectedThumbnailBorder, string TrackerForeground, decimal? CaptionOpacity, string CaptionFont, string TrackerFont, int? BorderRadius, int? Padding, bool? CaptionVisible, bool? ThumbnailsVisible, bool? TrackerVisible, bool? AutoPlay)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "UpdatePhotoGallery", 
                PhotoGalleryId, 
                ModuleId, 
                Title, 
                Source, 
                BasePath, 
                Width, 
                Height, 
                Background, 
                Foreground, 
                ButtonBackground, 
                ButtonForeground,
                ButtonBorder, 
                ButtonPlayForeground, 
                CaptionBackground, 
                CaptionForeground, 
                ContentBackground, 
                SelectedThumbnailBorder, 
                TrackerForeground, 
                CaptionOpacity, 
                CaptionFont, 
                TrackerFont, 
                BorderRadius, 
                Padding, 
                CaptionVisible, 
                ThumbnailsVisible, 
                TrackerVisible, 
                AutoPlay);
        }
        public override void DeletePhotoGallery(int PhotoGalleryId, int ModuleId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "DeletePhotoGallery", PhotoGalleryId, ModuleId);
        }
        public override IDataReader GetPhotoGallerySlides(int PhotoGalleryId)
        {
            return ((IDataReader)(SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "GetPhotoGallerySlides", PhotoGalleryId)));
        }
        public override int AddPhotoGallerySlide(int PhotoGalleryID, string Caption, string Image, string Thumb, int? OrderIndex, double? Scale, double? CenterX, double? CenterY, int? AnimationBegin, int? AnimationEnd, string BorderColor, int? BorderRadius, bool? BorderVisible, int? BorderWidth, int? Duration, int? EndDelay, double? RotateAngle)
        {
            return (Convert.ToInt32((SqlHelper.ExecuteScalar(ConnectionString, DatabaseOwner + ObjectQualifier + "AddPhotoGallerySlide", PhotoGalleryID, OrderIndex, Image, Thumb, Caption, Duration, EndDelay, AnimationBegin, AnimationEnd, CenterX, CenterY, Scale, RotateAngle, BorderVisible, BorderColor, BorderWidth, BorderRadius))));
        }
        public override void UpdatePhotoGallerySlide(int PhotoGallerySlideId, int PhotoGalleryID, string Caption, string Image, string Thumb, int? OrderIndex, double? Scale, double? CenterX, double? CenterY, int? AnimationBegin, int? AnimationEnd, string BorderColor, int? BorderRadius, bool? BorderVisible, int? BorderWidth, int? Duration, int? EndDelay, double? RotateAngle)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "UpdatePhotoGallerySlide", PhotoGallerySlideId, PhotoGalleryID, OrderIndex, Image, Thumb, Caption, Duration, EndDelay, AnimationBegin, AnimationEnd, CenterX, CenterY, Scale, RotateAngle, BorderVisible, BorderColor, BorderWidth, BorderRadius);
        }
        public override void DeletePhotoGallerySlide(int PhotoGallerySlideID)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "DeletePhotoGallerySlide", PhotoGallerySlideID);
        }
    }
}